import { PayloadAction, createSlice } from "@reduxjs/toolkit";
import { RootState } from ".";

export const tagsSlice = createSlice({
  name: 'tags',
  initialState: {
    value: Array<number>(),
  },

  reducers: {
    addTagId: (state, action: PayloadAction<number>) => {
      state.value.push(action.payload)
    },
    removeTag: (state, action: PayloadAction<number>) => {
      const index = state.value.indexOf(action.payload)
      if (index > -1) {
        state.value.splice(index, 1)
      }
    },
    clearTag: (state) => {
      state.value = []
    }
  }
})


export const { addTagId, removeTag, clearTag } = tagsSlice.actions
export default tagsSlice.reducer

export const tags = (state: RootState) => state.tags.value
